-- SpecialElemLord
-- Created by chendh Dec/14/2016
-- 特殊元素领主圣坛格子

require "game/ui/form/altar/UIElemAltar"

-- 内部函数声明
local redrawElemAltarItem;

-- 初始化
function initSpecialElemLord(self)
    --
    local icon = self.gridData.icon;
    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 打开元素圣坛界面
            local pos = self.gridData:getPos();
            local uiElemAltar = UIElemAltar.create(pos);
            UIMgr.getCurrentScene():removeFormByName("UIElemAltar");
            UIMgr.getCurrentScene():addForm(uiElemAltar);

            -- 音效
            AudioM.playFx("ui_open1");
        end
    end

    local iconPath = getLevelItemIconPath("elem_altar");
    if icon ~= nil then
        iconPath = getLevelItemIconPath(icon);
    end

    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;

    -- 图标往上偏移一点
    self.icon:setPositionY(10);

    -- 初始化水晶
    -- 1.左边
    local leftNode = cc.Node:create();
    leftNode:setName("left_node");
    self.icon:addChild(leftNode);
    leftNode:setPosition(cc.p(24, 65));

    -- 2.右边
    local rightNode = cc.Node:create();
    rightNode:setName("right_node");
    self.icon:addChild(rightNode);
    rightNode:setPosition(cc.p(104, 80));

    -- 3.中间
    local midNode = cc.Node:create();
    midNode:setName("mid_node");
    self.icon:addChild(midNode);
    midNode:setPosition(cc.p(42, 103));

    -- 打开透明度传递开关，保证子精灵的透明度会随着父节点变化而变化
    leftNode:setCascadeOpacityEnabled(true);
    rightNode:setCascadeOpacityEnabled(true);
    midNode:setCascadeOpacityEnabled(true);

    -- 绘制水晶
    redrawElemAltarItem(self);
end

-- 绘制元素圣坛的水晶
function redrawElemAltarItem(self, isFinal)
    local duration = 0;

    local iconImg   = self.icon;   -- 元素圣坛图标
    local leftNode  = findChildByName(iconImg, "left_node");
    local rightNode = findChildByName(iconImg, "right_node");
    local midNode   = findChildByName(iconImg, "mid_node");
    local iconNodeList = {
        { ["direction"] = "left",  ["node"] = leftNode,  ["icon_pos"] = {-2, 14}, },
        { ["direction"] = "right", ["node"] = rightNode, ["icon_pos"] = {2, 8}, },
        { ["direction"] = "mid",   ["node"] = midNode,   ["icon_pos"] = {0, 9}, },
    };

    -- 获取已经提交的元素
    local existedElems = ElementAltarM.getSpecialSubmittedElems(self.gridData:getPos());
    if isFinal then
        for _, info in pairs(iconNodeList) do
            local iconNode = info.node;
            -- 直接移除,子节点
            iconNode:removeAllChildren();
        end
    end

    local i = 0;
    for _, info in pairs(iconNodeList) do
        i = i + 1;
        local dir      = info.direction;
        local iconNode = info.node;
        local iconPos  = info.icon_pos;

        -- 如果该位置已经有水晶了，不处理
        if iconNode and iconNode:getChildrenCount() == 0 then
            local elem = existedElems[i];
            if not elem then
                iconNode:setVisible(false);
            else
                iconNode:setVisible(true);
                local index = self.gridData.elem_icon_index;
                if index < 0 then
                    trace("UIGrid", "未知的元素:" .. elem);
                end
                local iconName = string.format("%s_elem%d", dir, index);
                local iconPath = "images/ui/elem_altar/" .. iconName .. ".png"
                local elemIconImg = ccui.ImageView:create(iconPath);
                iconNode:addChild(elemIconImg);
                elemIconImg:setPosition(cc.p(iconPos[1], iconPos[2]));

                -- 播放水晶出现动画
                duration = 0.5;
                elemIconImg:setOpacity(0);
                local fadeIn = cc.FadeIn:create(duration);
                elemIconImg:runAction(fadeIn);
            end
        end
    end

    return duration;
end

-- 提交元素圣坛水晶的回调
function onSubmitElem(self, elem, isFinal)
    -- 绘制水晶(直接绘制)
    if isFinal then
        redrawElemAltarItem(self, isFinal);
    end

    local function doRedraw()
        return redrawElemAltarItem(self, isFinal);
    end
    self.actionManager:addAction(self.gridData, doRedraw, "doRedraw", {}, GRID_ACTION_PHASE_GRID_OPENED);
    EventMgr.fire(event.PLAYER_MOVE, self.gridData:getPos());
end

-- 元素圣坛奖励的回调
function onElemAltarBonus(self, bonus, disappear)
    if disappear then
        -- 播放建筑奖励特效
        buildingBonusEffect(self);
    end

    self:playBonusListEffect(bonus, disappear);
end

-- 绘制元素圣坛的水晶
function redrawFailSub(self, direction, elem)
    local iconImg   = self.icon;   -- 元素圣坛图标
    local leftNode  = findChildByName(iconImg, "left_node");
    local rightNode = findChildByName(iconImg, "right_node");
    local midNode   = findChildByName(iconImg, "mid_node");
    local iconPosList = {
        ["left"] = {-2, 14} ,
        ["right"] = {2, 8},
        ["mid"] = {0, 9},
    };

    local dirNode = {
        ["left"] = leftNode,
        ["right"] = rightNode,
        ["mid"] = midNode,
    };

    local iconNode =dirNode[direction];
    local dir      = direction;
    local iconPos  = iconPosList[direction];

    -- 获取已经提交的元素
    iconNode:setVisible(true);
    local index = self.gridData.elem_icon_index;
    if index < 0 then
        trace("UIGrid", "未知的元素:" .. elem);
    end
    local iconName = string.format("%s_elem%d", dir, index);
    local iconPath = "images/ui/elem_altar/" .. iconName .. ".png"
    local elemIconImg = ccui.ImageView:create(iconPath);
    iconNode:addChild(elemIconImg);
    elemIconImg:setPosition(cc.p(iconPos[1], iconPos[2]));

    -- 播放水晶出现动画
    elemIconImg:setOpacity(0);
    local fadeIn = cc.FadeIn:create(0.3);

    local callfunc = cc.CallFunc:create(function()
        -- 直接移除,子节点
        iconNode:removeAllChildren();
    end)

    elemIconImg:runAction(cc.Sequence:create(fadeIn, callfunc));
end